

cd "$raw/saves"

use "$raw/voter/ma_voterfile_after1980.dta", clear
 
drop residence_addresses_address_line  residence_addresses_extra residence_addresses_state *zip* date_confidence_description *1996* *1998* *2000*

foreach v of varlist general* presidential* {
	replace `v'="1" if `v'=="Y"|`v'=="D"|`v'=="R"
	destring `v', replace
	replace `v'= 0 if `v'==.
}

ren residence_addresses_city city_v
ren voters_birth_year birthyear_v

foreach v of varlist voters*name* city_v{
	replace `v'=trim(upper(`v'))
	}
	
g byte male_v = (voters_gender=="M")
drop voters_gen

encode party, generate(party_v)
drop party

gen DOB_v = date(voters_birth_date,"MDY")
format DOB_v %td
gen birthmonth_v=month(DOB_v)
gen birthday_v=day(DOB_v)

g reg_date_official =date( voters_calculated_reg_date ,"MDY")
g reg_date_calculated = date(voters_official_reg_date ,"MDY")


ren ethnic_d raceeth_v

ren voters_last lastname_v
ren voters_first firstname_v
ren voters_m 	mname_v
ren voters_name_suffix suffix_v
drop voters*
gen minitial_v=substr(mname,1,1)
gen finitial_v=substr(firstname,1,1)


order lalvoterid firstname mname minitial lastname suffix city male raceeth DOB birth* party

ren presidential_primary* primary*

egen ever_general=rowmax(general*)
egen ever_primary=rowmax(primary*)
egen ever_voter=rowmax(ever_general ever_primary)
gen ever_register = 1

label var ever_general "ever voted in general election"
label var ever_primary "ever voted in primary election"
label var ever_voter "ever voted"
label var ever_register "registered voter"

ren general_2016_11_08 general_8nov2016
ren general_2014_11_04 general_4nov2014
ren general_2012_11_06 general_6nov2012
ren general_2010_11_02 general_2nov2010
ren general_2008_11_04 general_4nov2008
ren general_2006_11_07 general_7nov2006
ren general_2004_11_02 general_2nov2004
ren general_2002_11_05 general_5nov2002
ren primary_2016_03_01 primary_1mar2016
ren primary_2012_03_06 primary_6mar2012
ren primary_2008_02_05 primary_2feb2008
ren primary_2004_03_02 primary_2mar2004

compress

save "$data/ma_voterfile_after1980_clean.dta", replace

